﻿namespace TanzimSaqib.NoBrainer.Sample.Tests
{
    using System;
    using DataAccess;
    using Controllers;
    using Xunit;
    using Moq;

    public class PostControllerTest
    {
        private readonly Post _Post;
        private readonly IPostController _Controller;
        private readonly Mock<ISingleRepository> _MockRepository = new Mock<ISingleRepository>();

        public PostControllerTest()
        {
            _Post = new Post
                        {
                            ID = 1,
                            UserId = new Guid("419C54A4-0E7B-4566-B2C3-39EE5A0C52D9"),
                            BlogId = 1,
                            UserName = "zim",
                            Title = "New post",
                            Body = "Body of the post",
                            TagList = "new, post"
                        };

            _Controller = new PostController(_MockRepository.Object);
        }

        [Fact]
        public void CreateTest()
        {
            _MockRepository.Setup(r => r.CreatePost(It.IsAny<Post>()))
                .Returns(It.IsAny<Post>())
                .Verifiable();

            var actual = _Controller.Create(_Post);

            _MockRepository.Verify(r => r.CreatePost(It.IsAny<Post>()), Times.Once());
            _MockRepository.Verify(r => r.CreateTags(It.IsAny<Post>()), Times.Between(0, 10, Range.Inclusive));
            _MockRepository.VerifyAll();

            Assert.True(actual.IsValid);
        }

        [Fact]
        public void GivenAPostId_CreatePostLoad_ShouldPopulateNewPostDefaultStatusAsPublished()
        {
            var actual = _Controller.CreatePostLoad(_Post);
            Assert.True(actual.Status == (int)Post.StatusEnum.Published);
        }

        [Fact]
        public void GivenAPostId_View_ReturnsAPopulatedValidPostToDisplay()
        {
            _MockRepository.Setup(r => r.GetPost(_Post))
                .Returns(_Post)
                .Verifiable();

            _MockRepository.Setup(r => r.GetUser(It.IsAny<Guid>()))
                .Returns(It.IsAny<aspnet_User>())
                .Verifiable();

            var actual = _Controller.View(_Post);
            
            _MockRepository.Verify(r => r.GetPost(It.IsAny<Post>()), Times.Once());
            _MockRepository.Verify(r => r.GetTagList(It.IsAny<Post>()), Times.Once());
            _MockRepository.Verify(r => r.GetUser(It.IsAny<Guid>()), Times.Once());
            _MockRepository.VerifyAll();

            Assert.True(actual.IsValid);
        }
    }
}
